package org.abratuhi.kvngroupware.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Date;


import org.abratuhi.kvngroupware.client.KVNGroupwareClient;
import org.abratuhi.kvngroupware.gui.GUIMain;
import org.abratuhi.kvngroupware.gui.GUIScenarioManager;
import org.abratuhi.kvngroupware.model.Brainstorm;
import org.abratuhi.kvngroupware.model.Scenario;
import org.abratuhi.kvngroupware.network.Message;
import org.jdom.Element;

public class MainController implements ActionListener, WindowListener, ItemListener{
	
	GUIMain app;
	KVNGroupwareClient kvnclient;
	
	public MainController(KVNGroupwareClient client, GUIMain app){
		this.kvnclient = client;
		this.app = app;
	}

	@Override
	public void windowActivated(WindowEvent e) {}

	@Override
	public void windowClosed(WindowEvent e) {}

	@Override
	public void windowClosing(WindowEvent e) {
		String[] attrNames = new String[]{"type", "action", "userId", "userPassword"};
		String[] attrValues= new String[]{"application", "logout", kvnclient.getUser().getId(), kvnclient.getUser().getPassword()};
		String text = new String();
		Element[] content = new Element[0];
		Message logoutMessage = kvnclient.createMessage(attrNames, attrValues, text, content);
		kvnclient.sendMessage(logoutMessage);
		
		kvnclient.exit();
	}

	@Override
	public void windowDeactivated(WindowEvent e) {}

	@Override
	public void windowDeiconified(WindowEvent e) {}

	@Override
	public void windowIconified(WindowEvent e) {}

	@Override
	public void windowOpened(WindowEvent e) {}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource().equals(app.buttonCreateBrainstorm)){
			if(app.fieldBrainstorm.getText() != null && app.fieldBrainstorm.getText() != ""){
				String[] attrNames = new String[]{"type", "action", "brainstormId", "userId", "teamId", "theme"};
				String[] attrValues= new String[]{"brainstorm", "create", 
						kvnclient.getUser().getId()+new Date().getTime(), 
						kvnclient.getUser().getId(), 
						kvnclient.getTeam().getId(), 
						app.fieldBrainstorm.getText()};
				String text = new String();
				Element[] content = new Element[0];
				
				Message message = kvnclient.createMessage(attrNames, attrValues, text, content);
				kvnclient.sendMessage(message);
				
				app.fieldBrainstorm.setText("");
			}
		}
		else if(e.getSource().equals(app.buttonJoinBrainstorm)){
			if(app.comboBrainstorms.getSelectedIndex() != -1){
				Brainstorm brainstorm = kvnclient.getBrainstorms().get(app.comboBrainstorms.getSelectedIndex());
				String cUser = kvnclient.getUser().getId();
				
				String[] attrNames = new String[]{"type", "action", "brainstormId", "userId", "teamId", "theme"};
				String[] attrValues= new String[]{"brainstorm", "join", brainstorm.getId(), cUser, brainstorm.getTeamId(), brainstorm.getTheme()};
				String text = new String();
				Element[] content = new Element[0];
				
				Message message = kvnclient.createMessage(attrNames, attrValues, text, content);
				kvnclient.sendMessage(message);
			}
		}
		else if(e.getSource().equals(app.buttonCreateChatroom)){
			if(app.fieldChatroom.getText() != null && app.fieldChatroom.getText() != ""){
				String[] attrNames = new String[]{"type", "action", "chatroomId", "userId"};
				String[] attrValues= new String[]{"chatroom", "create", app.fieldChatroom.getText(), kvnclient.getUser().getId()};
				String text = new String();
				Element[] content = new Element[0];
				
				Message message = kvnclient.createMessage(attrNames, attrValues, text, content);
				kvnclient.sendMessage(message);
				
				app.fieldChatroom.setText("");
			}
		}
		else if(e.getSource().equals(app.buttonJoinChatroom)){
			if(app.comboChatrooms.getSelectedIndex() != -1){
				String sChatroom = app.comboChatrooms.getSelectedItem().toString();
				String cUser = kvnclient.getUser().getId();
				
				String[] attrNames = new String[]{"type", "action", "chatroomId", "userId"};
				String[] attrValues= new String[]{"chatroom", "join", sChatroom, cUser};
				String text = new String();
				Element[] content = new Element[0];
				
				Message message = kvnclient.createMessage(attrNames, attrValues, text, content);
				kvnclient.sendMessage(message);
			}
		}
		else if(e.getSource().equals(app.buttonCreateScenario)){
			//if(app.comboScenarios.getSelectedIndex() != -1){
				String scenarioName = app.fieldScenario.getText();
				
				String[] attrNames = new String[]{"type", "action"};
				String[] attrValues= new String[]{"application", "create_scenario"};
				String text = new String();
				Element[] content = new Element[]{new Scenario(kvnclient.getUser().getTeamId(), scenarioName).toElement()};
				
				Message message = kvnclient.createMessage(attrNames, attrValues, text, content);
				kvnclient.sendMessage(message);
				
				app.fieldScenario.setText("");
			//}
		}
		else if(e.getSource().equals(app.buttonEditScenario)){
			int index = app.comboScenarios.getSelectedIndex();
			if(index != -1){
				Scenario scenario = kvnclient.getScenario(index);
				scenario.setGUI(new GUIScenarioManager(kvnclient, scenario, kvnclient.locale));
				scenario.getGUI().createJFrame();
			}
		}
		else if(e.getSource().equals(app.buttonJokesManager)){
			if(kvnclient.guijokemanager.getGUI() == null ||
					kvnclient.guijokemanager.getGUI().isVisible() == false)
				kvnclient.guijokemanager.createJFrame();
		}
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		if(e.getSource().equals(app.comboBrainstorms)){
			app.indexSelectedBrainstorm = app.comboBrainstorms.getSelectedIndex();
		}
		else if(e.getSource().equals(app.comboChatrooms)){
			app.indexSelectedChatroom = app.comboChatrooms.getSelectedIndex();
		}
		else if(e.getSource().equals(app.comboScenarios)){
			app.indexSelectedScenario = app.comboScenarios.getSelectedIndex();
		}
	}
}
